﻿using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace UFIDA.U8.YYNBJY.Operate
{
    public class TBOMMOMAIN
    {
        public static string MO_MOMAIN(string filename, string dbname, string cvouchid, bool ilog, bool iilog)
        {
            PUBMethod.PMethod.printmsgs(filename, "======================[" + DateTime.Now.ToString() + "][" + dbname + "][" + filename + "]方法START======================", true, ilog, iilog, System.Drawing.Color.Black);
            try
            {
                string condition = " and 1=1 ";
                if (!string.IsNullOrEmpty(cvouchid))
                {
                    condition += " and om.cCode='" + cvouchid + "' ";
                }
                string sql = @"select top 10 (ccode+'-'+cast(iVouchRowNo as nvarchar)) cCode,MODetailsID,om.cdepcode,md.cInvCode,iQuantity,dStartDate,dArriveDate,cMemo,'' cInvName,cVerifier,dVerifyDate 
                                from " + dbname+ @"..OM_MODetails md 
                                left join " + dbname + @"..om_momain om on md.moid = om.moid
                                left join " + dbname + @"..Inventory INV on md.cInvCode = inv.cInvCode 
                                where 1=1 and dDate>='2024-12-01' and isnull(cVerifier,'')<>'' and MODetailsID not in (select MODID from EF_LF_MESU8..EF_OMMOMAIN where DBNAME = '" + dbname + @"') " + condition + "";
                DataTable dt = DBHelp.SQLHelper.GetDataTable(sql);
                if (dt.Rows.Count > 0)
                {
                    JSONDATA.MOMCODE jsm = new JSONDATA.MOMCODE();
                    jsm.GUID = Guid.NewGuid().ToString();
                    jsm.TRXID = "WoDownLoad";
                    List<JSONDATA.DATAItem> lid = new List<JSONDATA.DATAItem>();
                    for (int i = 0; i < dt.Rows.Count; i++)
                    {
                        JSONDATA.DATAItem jda = new JSONDATA.DATAItem();
                        jda.WO_ID = dt.Rows[i]["cCode"].ToString();
                        jda.SEQ = dt.Rows[i]["MODetailsID"].ToString();
                        jda.WO_TYPE_ID = "OSC";
                        jda.ITEM_ID = dt.Rows[i]["cInvCode"].ToString();
                        jda.WO_QTY = dt.Rows[i]["iQuantity"].ToString();
                        jda.FACTORY_ID = dt.Rows[i]["cdepcode"].ToString();
                        jda.PLAN_START_PROCESS_DATE = dt.Rows[i]["dStartDate"].ToString();
                        jda.PLAN_COMP_DATE = dt.Rows[i]["dArriveDate"].ToString();
                        jda.CMT = dt.Rows[i]["cMemo"].ToString();
                        jda.CUMSTOMER_ITEM_CMT = dt.Rows[i]["cInvName"].ToString();
                        jda.DUTY_USER_ID = dt.Rows[i]["cVerifier"].ToString();
                        jda.DUTY_DATE_TIME = dt.Rows[i]["dVerifyDate"].ToString();
                        lid.Add(jda);
                    }
                    jsm.DATA = lid;
                    string json = JsonConvert.SerializeObject(jsm);
                    PUBMethod.PMethod.printmsgs(filename, "[" + dbname + "]调用["+filename+"]参数:" + json, false, ilog, iilog, System.Drawing.Color.Red);
                    string result = PUBMethod.PMethod.CreatePostHttpResponses(Model.PUBCLASS.MESURL, json, null);
                    PUBMethod.PMethod.printmsgs(filename, "[" + dbname + "][" + filename + "]返回参数:" + result, false, ilog, iilog, System.Drawing.Color.Red);
                    if (!string.IsNullOrEmpty(result))
                    {
                        if (result.Substring(0, 5) != "false")
                        {
                            JSONDATA.RESJSON jre = JsonConvert.DeserializeObject<JSONDATA.RESJSON>(result);
                            string GUID = jre.GUID;
                            string RTNCODE = jre.RTNCODE;
                            string RTNMSG = jre.RTNMSG;
                            if (RTNCODE == "0")
                            {
                                List<string> lis = new List<string>();
                                PUBMethod.PMethod.printmsgs(filename, "[" + dbname + "]["+filename+"]同步成功:" + RTNMSG, true, ilog, iilog, System.Drawing.Color.Green);
                                for (int i = 0; i < dt.Rows.Count; i++)
                                {
                                    string MoDId = dt.Rows[i]["MODetailsID"].ToString();
                                    string mocode = dt.Rows[i]["cCode"].ToString();
                                    lis.Add("insert into EF_LF_MESU8..EF_OMMOMAIN(DBNAME,MOCODE,MODID,CREATETIME,DEFINE1)values('" + dbname + "','" + mocode + "','" + MoDId + "',GETDATE(),'" + GUID + "') ");
                                }
                                DBHelp.SQLHelper.ExecuteSqlTran(lis);
                            }
                            else
                            {
                                PUBMethod.PMethod.printmsgs(filename, "[" + dbname + "][" + filename + "]同步失败:" + RTNMSG, true, ilog, iilog, System.Drawing.Color.Red);
                            }
                        }
                        else
                        {
                            PUBMethod.PMethod.printmsgs(filename, "[" + dbname + "][" + filename + "]接口返回异常:" + result, true, ilog, iilog, System.Drawing.Color.Red);
                        }
                    }
                    else
                    {
                        PUBMethod.PMethod.printmsgs(filename, "[" + dbname + "][" + filename + "]接口返回空", true, ilog, iilog, System.Drawing.Color.Red);
                    }
                }
                else
                {
                    PUBMethod.PMethod.printmsgs(filename, "[" + dbname + "]没有查到需要同步的["+filename+"]", true, ilog, iilog, System.Drawing.Color.Red);
                }
            }
            catch (Exception ex)
            {
                PUBMethod.PMethod.printmsgs(filename, "[" + filename + "]同步异常:" + ex.Message, true, ilog, iilog, System.Drawing.Color.Red);
            }
            PUBMethod.PMethod.printmsgs(filename, "======================[" + DateTime.Now.ToString() + "][" + dbname + "][" + filename + "]方法E N D======================", true, ilog, iilog, System.Drawing.Color.Black);
            return "";
        }
    }
}
